package com.wnhz.work.yz.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.meiqia.core.MQManager;
import com.meiqia.core.bean.MQMessage;
import com.meiqia.core.callback.OnGetMessageListCallback;
import com.meiqia.meiqiasdk.util.MQIntentBuilder;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.YaoQingBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.CircularImage;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YZServiceSelectActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yzservice_select);
        initTitle();
        initView();
    }

    private TextView tv_content;
    private TextView kefu;
    private CircularImage kefu_img;
    private TextView yaoqing_msg;

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("消息中心");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.ic_telephone).setOnClickListener(this);
        findViewById(R.id.ic_telephone).setVisibility(View.VISIBLE);
    }

    private void initView() {
        findViewById(R.id.phone_service).setOnClickListener(this);
        findViewById(R.id.online_service).setOnClickListener(this);
        findViewById(R.id.ic_telephone).setOnClickListener(this);
        findViewById(R.id.total).setOnClickListener(this);
        yaoqing_msg = (TextView) findViewById(R.id.yaoqing_msg);
        kefu_img = (CircularImage) findViewById(R.id.kefu_img);
        tv_content = (TextView) findViewById(R.id.tv_content);
        kefu = (TextView) findViewById(R.id.kefu);
        getInfo();
    }


    private void getInfo() {
        Date date = new Date();//取时间
        long timeStart = date.getTime();
        try {
            MQManager.getInstance(this).getMQMessageFromService(timeStart, 1, new OnGetMessageListCallback() {
                @Override
                public void onSuccess(List<MQMessage> list) {
                    if (list == null || list.size() == 0) return;
                    kefu.setText("" + list.get(0).getAgent_nickname());
                    tv_content.setText("" + list.get(0).getContent());
                    x.image().bind(kefu_img, list.get(0).getAvatar() + "");
                }

                @Override
                public void onFailure(int i, String s) {

                }
            });
        } catch (Exception w) {
            MyToast("请登录!");
            Intent intent = new Intent(this, LoginNewActivity.class);
            startActivity(intent);
        }
    }

    @Override
    protected void onResume() {
        getInfo();
        upLoad();
        super.onResume();
    }

    private List<YaoQingBean.InfoEntity> infos = new ArrayList<>();

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("token", MyApplication.getInstance().getUserBean().getToken());
        } catch (Exception e) {
            Intent intent = new Intent(this, LoginNewActivity.class);
            startActivity(intent);
            return;
        }
        showDialog();
        XUtil.Post(Url.TEAM_INVITEADDTEAMLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    infos.clear();
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YaoQingBean bean = gson.fromJson(result, YaoQingBean.class);
                        infos = bean.getInfo();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                if (infos.size() != 0) {
                    yaoqing_msg.setText("有" + infos.size() + "条通知");
                }
                findViewById(R.id.yaoqing).setOnClickListener(YZServiceSelectActivity.this);
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.ic_telephone:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("即将跳往拨打电话界面");

                builder.setTitle("客服");

                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent intent2 = new Intent(Intent.ACTION_DIAL);
                        Uri data = Uri.parse("tel:" + "4006658880");
                        intent2.setData(data);
                        startActivity(intent2);
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
                break;
            case R.id.total:
            case R.id.online_service:
                conversation();
                break;
            case R.id.yaoqing:
                Intent intent = new Intent(this, YaoQingActivity.class);
                ArrayList<YaoQingBean.InfoEntity> infoEntities = (ArrayList<YaoQingBean.InfoEntity>) infos;
                intent.putExtra("bean", infoEntities);
                startActivity(intent);
                break;
        }

    }

    @Override
    protected void onDestroy() {
        try {
            MQManager.getInstance(this).setClientOffline();
        } catch (Exception e) {

        }
        super.onDestroy();
    }

    private void conversation() {
        Intent intent;
        //                MQManager.getInstance(this).updateClientInfo(updateInfo, callback);

        try {
            HashMap<String, String> clientInfo = new HashMap<>();
            clientInfo.put("name", MyApplication.getInstance().getUserBean().getUserName());

            clientInfo.put("avatar", MyApplication.getInstance().getUserBean().getHeadUrl());
            clientInfo.put("tel", MyApplication.getInstance().getUserBean().getPhone());
            clientInfo.put("技能1", MyApplication.getInstance().getUserBean().getClassName());
            HashMap<String, String> updateInfo = new HashMap<>();

            upLoadInfo(updateInfo);
            intent = new MQIntentBuilder(this)
                    .setCustomizedId(MyApplication.getInstance().getUserBean().getPhone()) // 相同的 id 会被识别为同一个顾客
                    .setClientInfo(clientInfo)
                    .build();
        } catch (Exception e) {
            intent = new Intent(this, LoginNewActivity.class);
        }

        startActivity(intent);

    }

    private HashMap<String, String> upLoadInfo(HashMap<String, String> updateInfo) {
        Log.e("====", MyApplication.getInstance().getUserBean().getPhone());
        updateInfo.put("name", MyApplication.getInstance().getUserBean().getUserName());
        updateInfo.put("avatar", MyApplication.getInstance().getUserBean().getHeadUrl());
        updateInfo.put("tel", MyApplication.getInstance().getUserBean().getPhone());
        updateInfo.put("技能1", MyApplication.getInstance().getUserBean().getClassName());
        MQManager.getInstance(this).updateClientInfo(updateInfo, null);
        return updateInfo;
    }


}
